﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Domain.Entity;

namespace Pro260Website.Infrastructure.Filters
{
    public class YoutubeLinkParseAttribute : FilterAttribute, IActionFilter
    {
        public void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var modelData = filterContext.ActionParameters.SingleOrDefault(ap => ap.Key == "entity").Value;
            
            Platform p = new Platform();
            Review r = new Review();
            Game g = new Game();

            Type platformType = p.GetType();
            Type reviewType = r.GetType();
            Type gameType = g.GetType();

            if (modelData.GetType().FullName.Equals(platformType.FullName))
            {
                Platform model = (Platform)modelData;
                if (!string.IsNullOrEmpty(model.YoutubeLink) || !string.IsNullOrWhiteSpace(model.YoutubeLink))
                {
                    if (!model.YoutubeLink.Equals("http://www.youtube.com/v/"))
                    {
                        string link = model.YoutubeLink;

                        model.YoutubeLink = Parse(link);
                    }
                    else
                    {
                        model.YoutubeLink = string.Empty;
                    }
                }
            }
            else if (modelData.GetType().FullName.Equals(reviewType.FullName))
            {
                Review model = (Review)modelData;
                if (!string.IsNullOrEmpty(model.YoutubeLink) || !string.IsNullOrWhiteSpace(model.YoutubeLink))
                {
                    if (!model.YoutubeLink.Equals("http://www.youtube.com/v/"))
                    {
                        string link = model.YoutubeLink;

                        model.YoutubeLink = Parse(link);
                    }
                    else
                    {
                        model.YoutubeLink = string.Empty;
                    }
                }
            }
            else if (modelData.GetType().FullName.Equals(gameType.FullName))
            {
                Game model = (Game)modelData;
                if (!string.IsNullOrEmpty(model.YoutubeLink) || !string.IsNullOrWhiteSpace(model.YoutubeLink))
                {
                    if (!model.YoutubeLink.Equals("http://www.youtube.com/v/"))
                    {
                        string link = model.YoutubeLink;

                        model.YoutubeLink = Parse(link);
                    }
                    else
                    {
                        model.YoutubeLink = string.Empty;
                    }
                }
            }
        }

        public void OnActionExecuted(ActionExecutedContext filterContext)
        {
            //Do nothing
        }

        public string Parse(string input)
        {
            System.Text.RegularExpressions.Regex caseMost = new System.Text.RegularExpressions.Regex("[v][\\=][\\w \\W]{11}");
            System.Text.RegularExpressions.Regex caseBe = new System.Text.RegularExpressions.Regex("[e][\\/][\\w \\W]{11}");
            System.Text.RegularExpressions.Regex caseEmbed = new System.Text.RegularExpressions.Regex("[d][\\/][\\w \\W]{11}");
            System.Text.RegularExpressions.Regex caseInForm = new System.Text.RegularExpressions.Regex("[v][\\/][\\w \\W]{11}");

            string newLink = "http://www.youtube.com/v/";
            string temp = null;

            System.Text.RegularExpressions.Match match = null;

            //CaseMost
            if (match == null && input != null)
            {
                 match = caseMost.Match(input);
                 if (!string.IsNullOrWhiteSpace(match.Value) && !string.IsNullOrEmpty(match.Value))
                 {
                     temp = match.Value;
                 }
                 else
                 {
                     match = null;
                 }
             }
             //CaseBe
             if (match == null && input != null)
             {
                 match = caseBe.Match(input);
                 if (!string.IsNullOrWhiteSpace(match.Value) && !string.IsNullOrEmpty(match.Value))
                 {
                     temp = match.Value;
                 }
                 else
                 {
                     match = null;
                 }
             }
             //CaseEmbed
             if (match == null && input != null)
             {
                 match = caseEmbed.Match(input);
                 if (!string.IsNullOrWhiteSpace(match.Value) && !string.IsNullOrEmpty(match.Value))
                 {
                     temp = match.Value;
                 }
                 else
                 {
                     match = null;
                 }
             }
             //Link is already in the form needed
             if (match == null && input != null)
             {
                 match = caseInForm.Match(input);
                 if (!string.IsNullOrWhiteSpace(match.Value) && !string.IsNullOrEmpty(match.Value))
                 {
                     temp = match.Value;
                 }
                 else
                 {
                     match = null;
                 }
             }
             //Leaves Youtube's VideoID
             if (!string.IsNullOrWhiteSpace(temp))
             {
                 for (int i = 2; i < temp.Length; i++)
                 {
                     newLink = newLink + temp[i];
                 }
             }

             return newLink;
        }
    }
}